<?php
class FrontOrder
{
	public function __construct()
	{
		if(!isset($_SESSION['cartid']))
		{
			srand();
			$_SESSION['cartid']=rand();
			$cartid=$_SESSION['cartid'];
		} else {
			$cartid=$_SESSION['cartid'];
		}
		
		$this->cartid=$cartid;
	}
	public function cancelorder($order_id)
        {
            $sql = "call sp_cancelorder(".$order_id.")";
           // echo $sql;exit;
	    DB::exec($sql);
        }
	public function addOrderProcess()
	{
		$customer_id = $_SESSION['user_id'];
		$order_date = date('Y-m-d');
		$cartid = $this->cartid;
		
		$sql = "INSERT INTO tbl_orders(customer_id, order_date, order_status, deliverycharge, cart_id, ispaid, isdelivered)
				VALUES(" . $customer_id . ", now() , 0, 0, '" . $this->cartid . "', '0', '0')";
		DB::exec($sql);
	}
	
	public function lastInsertedOrderId()
	{
		$sql = "SELECT * FROM tbl_orders WHERE order_id in (select max(order_id) from tbl_orders)";            
		$last_insert_id = DB::fetchRow($sql);
		
		return $last_insert_id;
	}
        public function getextranetorderlist1(){

            $sql = "SELECT * FROM tbl_orders where customer_id=".$_SESSION["user_id"]." and ispaid=0 and isdelivered=0 and iscancelled=0 and isprocessing=0 order by order_date desc ";
          //  echo $sql;
            $orderlist=DB::fetchall($sql);
           // print_r($orderlist);
            return $orderlist;
        }
        
        public function getextranetorderlistinprocess(){

            $sql = "SELECT * FROM tbl_orders where customer_id=".$_SESSION["user_id"]." and ispaid=0 and isdelivered=0 and iscancelled=0 and isprocessing=1 order by order_date desc ";
          //  echo $sql;
            $orderlist=DB::fetchall($sql);
           // print_r($orderlist);
            return $orderlist;
        }



                public function getextranetorderlist2(){

            $sql = "SELECT * FROM tbl_orders where customer_id=".$_SESSION["user_id"]." and ispaid=1 and isdelivered=0 and iscancelled=0  order by order_date desc";
          //  echo $sql;
            $orderlist=DB::fetchall($sql);
           // print_r($orderlist);
            return $orderlist;
        }
                public function getextranetorderlist3(){

            $sql = "SELECT * FROM tbl_orders where customer_id=".$_SESSION["user_id"]." and ispaid=0 and isdelivered=1 and iscancelled=0 order by order_date desc ";
          //  echo $sql;
            $orderlist=DB::fetchall($sql);
           // print_r($orderlist);
            return $orderlist;
        }
                public function getextranetorderlist4(){

            $sql = "SELECT * FROM tbl_orders where customer_id=".$_SESSION["user_id"]." and ispaid=1 and isdelivered=1 and iscancelled=0 order by order_date desc  ";
          //  echo $sql;
            $orderlist=DB::fetchall($sql);
           // print_r($orderlist);
            return $orderlist;
        }
         public function getextranetOrderByID($orderid)
        {
            $sql = "SELECT * from tbl_orders o inner join tbl_user u on o.customer_id=u.user_Id where o.order_id=".$orderid. " ";
          // echo $sql;
	    $item_arr = DB::fetchRow($sql);
  // print_r($item_arr);
		return $item_arr;
        }
          public function getCart($cartid)
       {
		$sql = "SELECT p.productid,p.item_code,s.price,p.product_title,s.quantity from  tbl_shoppingcart s inner join tbl_products p on p.productid=s.product_id  where cart_id=".$cartid."" ;
             //   echo $sql;//exit;
		$cart = DB::fetchAll($sql);
 //  print_r($cart);
                return $cart;
       }
       public function iscredituser($userid){
        $sql = "SELECT * from  tbl_user  where user_id =".$userid."  and iscreditor=1 ";
        //echo $sql;
        $userarr = DB::fetchRow($sql);

        if($userarr){
            return true;
        }else{
            return false;
        }
    }
     public function creditbalance($userid){
        $accountbalance=0;
        $sql = "SELECT  *  from  tbl_creditaccount  where userid =".$userid." ";
        $credit= DB::fetchAll($sql);
        foreach($credit as $arr){
         if(in_array($arr["transtype"], array(0,1,4,8))){
            $accountbalance+=(float)$arr["transvalue"];
         }
         else{
            $accountbalance-=(float)$arr["transvalue"];

         }
        }
        return $accountbalance;
    }

	
}
?>